import Vue from 'vue';
import $ from 'jquery';
import app from './app.vue';
import './lib/mui/css/mui.css';
import 'bootstrap/dist/css/bootstrap.css';
import './lib/mui/css/icons-extra.css';
//导入
import VueRouter from 'vue-router';
//注册
Vue.use(VueRouter);
//导入自定义router
import router from './router.js';
// import { Swipe, SwipeItem, Button, Header } from 'mint-ui';
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.component(Header.name, Header);
import MintUI from 'mint-ui';
Vue.use(MintUI);
import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.http.options.root = 'http://192.168.2.120:3000';
//引入格式化时间模块
import moment from 'moment';
import VuePreview from 'vue-preview';
Vue.use(VuePreview);
Vue.use(VuePreview, {
    mainClass: 'pswp--minimal--dark',
    barsSize: {top: 0, bottom: 0},
    captionEl: false,
    fullscreenEl: false,
    shareEl: false,
    bgOpacity: 0.85,
    tapToClose: true,
    tapToToggleControls: false
});
import './css/myMintUi.scss';
import Vuex from 'vuex';
Vue.use(Vuex);
var car = JSON.parse(localStorage.getItem('car')) || [];
var store = new Vuex.Store({
    state:{
        car:car
    },
    mutations:{
        updateSel:function (state,obj) {
            state.car.some((item)=>{
                if(item.id == obj.id){
                    item.selected = obj.sel;
                    return true;
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        deleteCar:function (state,obj) {
            state.car.some((item,i)=>{
                if(item.id == obj.id){
                    state.car.splice(i,1);
                    return true;
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        goodsUpdating:function (state,obj) {
            state.car.some(item=>{
                if(item.id == obj.id){
                    item.count = obj.count;
                    return true;
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        addToCar:function (state,addObj) {
            var bol1 = false;
            state.car.some((item)=>{
                if(item.id == addObj.id){
                    item.count = parseInt(item.count) + parseInt(addObj.count); 
                    bol1 = true;
                    return true;
                }
            });
            if(!bol1){
                state.car.push(addObj);
                localStorage.setItem('car',JSON.stringify(state.car));
            }
        }
    },
    getters:{
        getAllCount:function (state) {
            var count = 0
            state.car.forEach((item)=>{
                count += parseInt(item.count);
            });
            return count;
        },
        getAllResult:function (state) {
            var result = 0;
            var res_count = 0;
            // var res_sel = [];
            state.car.forEach(item=>{
                if(item.selected == true){
                    result += parseInt(item.count)*parseInt(item.price);
                    res_count += parseInt(item.count);
                    // res_sel.push(item.id);
                }
            });
            // return {result,res_count,res_sel};
            return {result,res_count};
        }
    }
})
//创建全局过滤器
Vue.filter('formatTime', function (msg,arg) {
    return moment(msg).format(arg);
})
$(function () {
    var vm = new Vue({
        el:'#app',
        render:c=>c(app),
        router,
        store,
    });
});